<template>
  <j-modal
    :confirmLoading="confirmLoading"
    :dialogStyle="dialogStyle"
    :title="title"
    :visible="visible"
    :width="800"
    cancelText="关闭"
    switchFullscreen
    @cancel="handleCancel"
    @ok="handleOk">
    <a-spin :spinning="confirmLoading">
      <a-form :form="form" :rules="validatorRules">
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="年份">
          <a-input v-decorator="['year', {
            rules: [
              {
                required: true,
                message: '年份不能为空!',
              },
              // {
              //   validator: validateToNextPassword,
              // },
            ],
            }]" placeholder="请输入年份" suffix="年" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="能源消费总量">
          <a-input v-decorator="['totalEnergyConsumption', {
            rules: [
              {
                required: true,
                message: '仅能输入数字和两位小数，不能为空或为负数！',
                pattern: /^[0-9]+\.?[0-9]{0,2}$/
              },
            ],
          }]" placeholder="请输入能源消费总量" suffix="tce" />
        </a-form-item>
<!--        <a-form-item-->
<!--          :labelCol="labelCol"-->
<!--          :wrapperCol="wrapperCol"-->
<!--          label="能源消费总量同比">-->
<!--          <a-input v-decorator="['totalEnergyConsumptionYoy', {-->
<!--            rules: [-->
<!--              {-->
<!--                required: true,-->
<!--                message: '仅能输入1个负号、数字和两位小数，不能为空！',-->
<!--                pattern: /^(-|\+)?[0-9]+\.?[0-9]{0,2}$/-->
<!--              },-->
<!--            ],-->
<!--          }]" placeholder="请输入能源消费总量同比" suffix="%" />-->
<!--        </a-form-item>-->
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="碳排放总量">
          <a-input v-decorator="['totalCarbonEmissions', {
            rules: [
              {
                required: true,
                message: '仅能输入数字和两位小数，不能为空或为负数！',
                pattern: /^[0-9]+\.?[0-9]{0,2}$/
              },
            ],
          }]" placeholder="请输入碳排放总量" suffix="tco2" />
        </a-form-item>
<!--        <a-form-item-->
<!--          :labelCol="labelCol"-->
<!--          :wrapperCol="wrapperCol"-->
<!--          label="碳排放总量同比">-->
<!--          <a-input v-decorator="['totalCarbonEmissionsYoy', {-->
<!--            rules: [-->
<!--              {-->
<!--                required: true,-->
<!--                message: '仅能输入1个负号、数字和两位小数，不能为空！',-->
<!--                pattern: /^(-|\+)?[0-9]+\.?[0-9]{0,2}$/-->
<!--              },-->
<!--            ],-->
<!--          }]" placeholder="请输入碳排放总量同比" suffix="%" />-->
<!--        </a-form-item>-->
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="能耗强度">
          <a-input v-decorator="['energyConsumptionIntensity', {
            rules: [
              {
                required: true,
                message: '仅能输入数字和两位小数，不能为空或为负数！',
                pattern: /^[0-9]+\.?[0-9]{0,2}$/
              },
            ],
          }]" placeholder="请输入能耗强度" suffix="tce/万元" />
        </a-form-item>
<!--        <a-form-item-->
<!--          :labelCol="labelCol"-->
<!--          :wrapperCol="wrapperCol"-->
<!--          label="能耗强度同比">-->
<!--          <a-input v-decorator="['energyConsumptionIntensityYoy', {-->
<!--            rules: [-->
<!--              {-->
<!--                required: true,-->
<!--                message: '仅能输入1个负号、数字和两位小数，不能为空！',-->
<!--                pattern: /^(-|\+)?[0-9]+\.?[0-9]{0,2}$/-->
<!--              },-->
<!--            ],-->
<!--          }]" placeholder="请输入能耗强度同比" suffix="%" />-->
<!--        </a-form-item>-->
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="碳排放强度">
          <a-input v-decorator="['carbonEmissionIntensity', {
            rules: [
              {
                required: true,
                message: '仅能输入数字和两位小数，不能为空或为负数！',
                pattern: /^[0-9]+\.?[0-9]{0,2}$/
              },
            ],
          }]" placeholder="请输入碳排放强度" suffix="tco2/万元" />
        </a-form-item>
<!--        <a-form-item-->
<!--          :labelCol="labelCol"-->
<!--          :wrapperCol="wrapperCol"-->
<!--          label="碳排放强度同比">-->
<!--          <a-input v-decorator="['carbonEmissionIntensityYoy', {-->
<!--            rules: [-->
<!--              {-->
<!--                required: true,-->
<!--                message: '仅能输入1个负号、数字和两位小数，不能为空！',-->
<!--                pattern: /^(-|\+)?[0-9]+\.?[0-9]{0,2}$/-->
<!--              },-->
<!--            ],-->
<!--          }]" placeholder="请输入碳排放强度同比" suffix="%" />-->
<!--        </a-form-item>-->
      </a-form>
    </a-spin>
  </j-modal>
</template>

<script>
import pick from 'lodash.pick'
import { postAction } from '@api/manage'
import Vue from 'vue'
import { LACK } from '@/store/mutation-types'

export default {
  name: 'trendAddEdit',
  data() {
    return {
      confirmLoading: false,
      title: '',
      visible: false,
      form: this.$form.createForm(this),
      dialogStyle: {
        top: '200px'
        // right:"20px",
      },
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      validatorRules: {
        totalEnergyConsumption: {
          rules: [{ required: true, trigger: 'change', message: '请输入数字！', pattern: '^\d{1,}\.\d{1,}|\d{1,}]/g' }]
        }
      },
      url: {
        add: '/carbon/carbonOverviewYearData/add',
        edit: '/carbon/carbonOverviewYearData/edit'
      },
    }
  },
  created() {
  },
  methods: {
    add() {
      this.edit({})
    },
    edit(record) {
      console.log(record)
      this.visible = true
      this.form.resetFields()
      this.model = Object.assign({}, record)
      this.$nextTick(() => {
        this.form.setFieldsValue(pick(
          this.model,
          'year',
          'totalEnergyConsumption',
          'totalEnergyConsumptionYoy',
          'totalCarbonEmissions',
          'totalCarbonEmissionsYoy',
          'energyConsumptionIntensity',
          'energyConsumptionIntensityYoy',
          'carbonEmissionIntensity',
          'carbonEmissionIntensityYoy'))
        // year: this.model.batteryyearNumber,
      })
    },
    close() {
      this.$emit('close')
      this.visible = false
    },
    handleCancel() {
      this.close()
    },
    handleOk() {
      let that = this
      this.form.validateFields((err, values) => {
        if (!err) {
          let httpurl = ''
          this.confirmLoading = true
          console.log(this.model)
          if (!this.model.id) {
            httpurl += this.url.add
          } else {
            httpurl += this.url.edit
          }
          console.log(httpurl)
          let formData = Object.assign(this.model, values)
          formData.areaCode = '021'
          postAction(httpurl, formData).then(res => {
            if (res.success) {
              that.$message.success(res.message)
              that.$emit('refresh')
            } else {
              that.$message.error(res.message)
            }
          }).finally(() => {
            that.confirmLoading = false
            that.close()
            that.$parent.$parent.loadData()
          })
        }
      })
    }
  }
}
</script>

<style scoped>

</style>